************* GRAPH 1: 4 item PostMaterialism  vs Competition Index ************

*** Install estout if not already installed
* ssc install estout

** Load the World Value Survey dataset
use "${raw}/Trends_VS_1981_2022_stata_v4_0", clear

** Set up the svy environment with population weights
svyset S007 [pweight=S018]

keep if Y002>0 
keep S007 S003 Y002 s002 S018 

* S007 Respondent number
* S003 Country code
* Y002 4 item post materialism index
* s002 wave
* S018 equilibrated weight 1000


** Compute aggregate statistics with survey weights for 4item PostMaterialism
collapse (mean) Y002 [pweight=S018], by(S003) //country weighted mean // acrooss time and people
rename S003 country_code 

** Merge with the competition index dataset
merge 1:1 country_code using "${dta}/comp_idx.dta"
keep if _merge == 3 
drop _merge country

** Graph: 4i Post Materialism vs Competition index
graph set window fontface "Times"
set scheme s1mono
twoway (scatter Y002 comp_idx, mlabel(country_code) mlabsize(tiny) mcolor(black) mlabcolor(black) msymbol(o)) ///
       (lfit Y002 comp_idx, lcolor(black)), ///
       ytitle("Post Materialism Index") ///
       xtitle("Competition Index") ///
       legend(off) //
	
	   
graph export "${graphs_path}/4iPMvsCOMP.pdf", as(pdf) replace name("Graph")

** Create a Temporary File Name

tempfile temp_file  // Creates a temporary file name and stores it in the macro `temp_file`

** Generate the Dummy Variable:
** Calculate the median of `comp_idx` using `egen`
egen median_comp_idx = median(comp_idx)

** Generate `comp_dummy` based on whether `comp_idx` is greater than or equal to the median
gen comp_dummy = (comp_idx >= median_comp_idx)

** Drop unnecessary variables
drop median_comp_idx Y002 comp_idx

** Save the current dataset to the temporary file
save "`temp_file'", replace  // Note the use of backticks and quotes to reference the macro

** Reload the Raw Dataset
use "${raw}/Trends_VS_1981_2022_stata_v4_0", clear

** Define the survey settings with population weights
svyset S007 [pweight=S018]

** Prepare the Raw Data for Merging:
** Keep observations where Y002 > 0
keep if Y002 > 0 
** Keep only the necessary variables
keep S007 S003 s002 Y002 S018 
** Rename `S003` to `country_code`
rename S003 country_code 

** Merge with the Temporary File
** Merge the raw data with the temporary file based on `country_code`
merge m:1 country_code using "`temp_file'"

** Keep only matched observations:
keep if _merge == 3  // Keeps only observations present in both datasets
** Drop the merge indicator variable
drop _merge

** Keep only observations from the 5th wave (2009 onward) to match years of comp_idx
keep if s002>=5

** Regression and storage of the results  
svy: reg Y002 comp_dummy
eststo model1 

** LaTex Export of the table
esttab model1 using "${tables_path}/regressionPM_results.tex", replace ///
    title("Regression Results of Y002 over Competition Dummy") ///
    label ///
    se ///
    starlevels(* 0.10 ** 0.05 *** 0.01) ///
    varlabels(comp_dummy "Competition Dummy") ///
    addnotes("Survey-weighted regression") 

******************* GRAPH 2: Care Index vs Competition Index *******************
** Load the World Value Survey dataset
use "${raw}/Trends_VS_1981_2022_stata_v4_0", clear

** Set up the svy environment with population weights
svyset S007 [pweight=S018]

keep if A193>0 
keep S007 S003 A193 s002 S018 

** Compute aggregate statistics with survey weights for Care Index
collapse (mean) A193 [pweight=S018], by(S003)
rename S003 country_code 

** Merge with the competition index dataset
merge 1:1 country_code using "${dta}/comp_idx.dta"
keep if _merge == 3 
drop _merge country

** Graph:Care index vs Competition index
set scheme s1mono
graph set window fontface "Times"
twoway (scatter A193 comp_idx, mlabel(country_code) mlabsize(tiny) mcolor(black) mlabcolor(black) msymbol(o)) ///
       (lfit A193 comp_idx, lcolor(black)), ///
       ytitle("Care Index") ///
       xtitle("Competition Index") ///
       legend(off) //

	
	   
graph export "${graphs_path}/CAREvsCOMP.pdf", as(pdf) replace name("Graph")

** Create a Temporary File Name
tempfile temp_file  // Creates a temporary file name and stores it in the macro `temp_file`

** Generate the Dummy Variable:
** Calculate the median of `comp_idx` using `egen`
egen median_comp_idx = median(comp_idx)

** Generate `comp_dummy` based on whether `comp_idx` is greater than or equal to the median
gen comp_dummy = (comp_idx >= median_comp_idx)

** Drop unnecessary variables
drop median_comp_idx A193 comp_idx

** Save the current dataset to the temporary file
save "`temp_file'", replace  // Note the use of backticks and quotes to reference the macro

** Reload the Raw Dataset for the regression 
use "${raw}/Trends_VS_1981_2022_stata_v4_0", clear

** Define the survey settings with population weights
svyset S007 [pweight=S018]

** Prepare the Raw Data for Merging:
keep if A193 > 0 
** Keep only the necessary variables
keep S007 S003 s002 A193 S018 
** Rename `S003` to `country_code`
rename S003 country_code 

** Merge with the Temporary File
** Merge the raw data with the temporary file based on `country_code`
merge m:1 country_code using "`temp_file'"

** Keep only matched observations:
keep if _merge == 3  // Keeps only observations present in both datasets
** Drop the merge indicator variable
drop _merge

** Regression and storage of the results  
svy: reg A193 comp_dummy
eststo model2

** LaTex Export of the table
esttab model2 using "${tables_path}/regressionCare_results.tex", replace ///
    title("Regression Results of A193 over Competition Dummy") ///
    label ///
    se ///
    starlevels(* 0.10 ** 0.05 *** 0.01) ///
    varlabels(comp_dummy "Competition Dummy") ///
    addnotes("Survey-weighted regression") 

